package cn.site.core.orm;

import java.io.Serializable;

/**
 * 分页参数封装类.
 */
public class PageRequest implements Serializable {

    private static final long serialVersionUID = 1L;

    protected int pageNo = 1;
    protected int pageSize = 20;

    protected boolean countTotal = true;

    public PageRequest() {
    }

    public PageRequest(int pageNo, int pageSize) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }

    /**
     * 获得当前页的页号, 序号从1开始, 默认为1.
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * 设置当前页的页号, 序号从1开始, 低于1时自动调整为1.
     */
    public void setPageNo(final int pageNo) {
        this.pageNo = pageNo;

        if (pageNo < 1) {
            this.pageNo = 1;
        }
    }

    /**
     * 获得每页的记录数量, 默认为10.
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 设置每页的记录数量, 低于1时自动调整为1.
     */
    public void setPageSize(final int pageSize) {
        this.pageSize = pageSize;
        if (pageSize < 1) {
            this.pageSize = 1;
        }
    }

    /**
     * 是否默认计算总记录数.
     */
    public boolean isCountTotal() {
        return countTotal;
    }

    /**
     * 设置是否默认计算总记录数.
     */
    public void setCountTotal(boolean countTotal) {
        this.countTotal = countTotal;
    }

    /**
     * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置, 序号从0开始.
     */
    public int getOffset() {
        return ((pageNo - 1) * pageSize);
    }


    //用于jquery easyui的grid
    protected int rows = 1;
    protected int page = 10;

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
        setPageSize(rows);
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
        setPageNo(page);
    }

    @Override
    public boolean equals(Object obj) {

        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return String.valueOf(pageNo + pageSize).hashCode();
    }
}
